__m.messageModule={
    
    /***************************************************************************
     * Sayfayı Yükle
     */
    init:function(){
        var $minHeight=$("body").height()-$(".footer").height() ;
        var $scrollElem=$("#panelUsers");
        var $msgPane=$("#msgPane");
        if($scrollElem.height()<$minHeight){
            $scrollElem.height($minHeight);
        }
        if($msgPane.length>0){
            $msgPane.height($scrollElem.height());
            $("#scrollPane").height($scrollElem.height()-$("#panelSending").height());
            
        }
    }
};
__m.messageModule.scroll={
    $scrollArea:null,    
    $scrollMain:null, 
    $scrollElem:null,    
    $baseElem:null,    
    $areaHeight:0,    
    $barHeight:0,    
    $baseHeight:0,
    Enabled:true,
    init:function(){        
        this.$scrollArea=$(".scroll-area");        
        this.$scrollMain=$(".scroll-main");
        this.$scrollElem=$(".scroll-scroll-elem");
        this.$baseElem=$(".scroll-base");
        
        this.$areaHeight=this.$scrollArea.height();
        this.$baseHeight=this.$baseElem.height();
        this.$barHeight=(this.$areaHeight) * (this.$areaHeight / this.$baseHeight);
        this.$scrollElem.height(this.$barHeight);
        this.$baseElem.css("top",0);
        this.$scrollElem.css("top",0);
        
        var stop=function(){
            return false;
        }

        var oldMouseCoordY=null;
        var MouseDownEvent=function(){
            $(document).bind("mousemove",MouseMoveEvent);
            $(document).bind("mouseup",MouseUpEvent);
            $("body").addClass("unselectable");
        }
        
        var MouseMoveEvent=function(e){            
            var MouseCoordY=e.clientY;
            var delta=null;
            if(oldMouseCoordY==null){
                oldMouseCoordY=MouseCoordY;
            }else{
                delta=MouseCoordY-oldMouseCoordY;
                oldMouseCoordY=MouseCoordY;                
                __m.messageModule.scroll.setScroll(-delta * (__m.messageModule.scroll.$baseHeight/__m.messageModule.scroll.$areaHeight));
            }
        }
        
        var MouseUpEvent=function(){
            $(document).unbind("mousemove", MouseMoveEvent);
            $(document).unbind("mouseup", MouseUpEvent);
            $("body").removeClass("unselectable");
            oldMouseCoordY=null;
        }
        
        this.$baseElem.hover(function(){
            $(document).bind("mousewheel DOMMouseScroll",stop);
        },function(){
            $(document).unbind("mousewheel DOMMouseScroll",stop);
        });
        
        this.$baseElem.bind("mousewheel DOMMouseScroll",function(event){            
            if (event.type == 'mousewheel') {
                __m.messageModule.scroll.setScroll(event.originalEvent.wheelDelta);
            }
            else if (event.type == 'DOMMouseScroll') {
                __m.messageModule.scroll.setScroll(-event.originalEvent.detail*40);
            }
        });
        this.$scrollElem.mousedown(MouseDownEvent);
        this.$scrollElem.mouseup(MouseUpEvent);
        
        $("#btnSmt").click(function(){
            var elem="<div style='height:150px'>asdasd</div>";
            __m.messageModule.scroll.$baseElem.append(elem);
        });
        
       window.setInterval(function(){
           __m.messageModule.scroll.refresh();
       }, 100);
        
    },
    refresh:function(){
        this.$areaHeight=this.$scrollArea.height();
        this.$baseHeight=this.$baseElem.height();
        this.$barHeight=(this.$areaHeight) * (this.$areaHeight / this.$baseHeight);
        this.$scrollElem.height(this.$barHeight);
    },
    setScroll:function(size){
        if(!this.Enabled) return;
        var currentPos=parseInt(this.$baseElem.css("top").replace("px",""));
        var scrPos=parseInt(this.$scrollElem.css("top").replace("px",""));
      
        var newPos=currentPos+size;
        var newScrPos=scrPos - size*this.$areaHeight/this.$baseHeight;
        if(newPos>=0){
            this.$baseElem.css("top", 0);
            this.$scrollElem.css("top", 0);
        }else if(newPos<=-1*(this.$baseHeight-this.$areaHeight)){
            var sah=this.$areaHeight;            
            var sh=sah * this.$areaHeight/this.$baseHeight;            
            this.$scrollElem.height(sh);
            this.$baseElem.css("top", -1*(this.$baseHeight-this.$areaHeight));
            this.$scrollElem.css("top",sah-sh);
        }else{
            this.$baseElem.css("top", newPos);
            if(newScrPos<0)
                this.$scrollElem.css("top", 0);
            else if(newScrPos>this.$areaHeight-this.$areaHeight* this.$areaHeight/this.$baseHeight){
                this.$scrollElem.css("top", this.$areaHeight-this.$areaHeight* this.$areaHeight/this.$baseHeight);
                this.$baseElem.css("top", -1*(this.$baseHeight-this.$areaHeight));
            }
            else this.$scrollElem.css("top", newScrPos);
        }
    }
};

$(document).ready(function(){
  
   __m.pageHeader.simpleInit();
   __m.messageModule.init();
   __m.messageModule.scroll.init();
   
});
